package com.qiji.video.web.controller;

import com.github.pagehelper.PageInfo;
import com.qiji.video.bean.BrowsingHistory;
import com.qiji.video.bean.Collection;
import com.qiji.video.bean.Users;
import com.qiji.video.bean.Videos;
import com.qiji.video.bean.vo.BrowsingHistoryAndVideo;
import com.qiji.video.bean.vo.CollectionAndVideo;
import com.qiji.video.service.IUserControlService;
import com.qiji.video.util.Md5Util;
import com.qiji.video.util.Result;
import com.qiji.video.util.ResultCode;
import com.qiji.video.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户模块
 */
@Api(
        tags = {"用户管理模块"}
)
@RestController
@RequestMapping({"/auth/admin/user"})
public class UserController {
    @Autowired
    private IUserControlService service;
    //用户查询（分表）
    @ApiOperation("用户查询")
    @GetMapping("/queryUser")
    public Result queryUser(Integer pageNum,Integer pageSize){
        PageInfo<Users> info = service.pageQuery(pageNum, pageSize);
        return ResultUtil.success(info);
    }
    @ApiOperation("无参用户查询")
    @GetMapping("/users")
    public Result users(){
        return ResultUtil.success(service.selsct());
    }
    //用户查询（id）
    @ApiOperation("ID查询用户")
    @GetMapping("/selectById")
    public Result selectById(Integer pageNum,Integer pageSize,Integer id){
        PageInfo<Users> info = service.selectUserById(pageNum, pageSize, id);
        return ResultUtil.success(info);
    }
    //修改信息
    @ApiOperation("修改信息")
    @PostMapping("/updateUser")
    public Result updateUser(Users users){
        service.updateUserMagsById(users);
        return ResultUtil.success();
    }
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id"),
            @ApiImplicitParam(name = "password",value = "修改后的密码")
    })
    //修改密码
    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public Result updatePassword(@ApiIgnore Users users){
        users.setPassword(Md5Util.getMd5Key(users.getPassword()));
        service.upadateUserPasswordById(users);
        return ResultUtil.success();
    }
    //删除用户
    @ApiOperation("删除用户")
    @DeleteMapping("/deleteUser")
    public Result daleteUser(Integer id){
        service.daleteUserById(id);
        return ResultUtil.success();
    }
    @ApiOperation("添加用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "姓名"),
            @ApiImplicitParam(name = "phone",value = "电话号码"),
            @ApiImplicitParam(name = "password",value = "密码"),
            @ApiImplicitParam(name = "headPortrait",value = "头像"),
            @ApiImplicitParam(name = "birthday",value = "生日"),
            @ApiImplicitParam(name = "email",value = "邮箱"),
    })
    @PostMapping("/insertUser")
    public Result insertUser(Users users){
        users.setPassword(Md5Util.getMd5Key(users.getPassword()));
        service.insertUser(users);
        return ResultUtil.success();
    }
    @ApiOperation("用户ID查询浏览历史")
    @GetMapping("/selectBHById")
    public Result selectBHById(Integer id){
        List<BrowsingHistoryAndVideo> list = service.selectBVById(id);
        return new Result(ResultCode.SELECT_SUCCESS,list);
    }

    @ApiOperation("id删除浏览历史")
    @DeleteMapping("/delectBHById")
    public Result delectBHById(Integer id){
        service.delectBHById(id);
        return ResultUtil.success();
    }

    @ApiOperation("用户ID查询收藏电影")
    @GetMapping("/selectClById")
    public Result selectClById(Integer id){
        List<CollectionAndVideo> list = service.selectCAById(id);
        return new Result(ResultCode.SELECT_SUCCESS,list);
    }

    @ApiOperation("id删除收藏电影")
    @DeleteMapping("/delectCLById")
    public Result delectCLById(Integer id){
        service.delectCLById(id);
        return ResultUtil.success();
    }
}
